<?php

//$Mail = new MailClass('wilhelm.murdoch@gmail.com', 'subject', 'you suck');

//$Mail->send('html');

class MailClass {

	const CLRF = "\r\n";
	const PRIORITY_LOW = 4;
	const PRIORITY_NORMAL = 3;
	const PRIORITY_HIGH = 2;

	private $mail_headers;
	private $mail_subject;
	private $mail_message;
	private $mail_priority;
	private $mail_recipients;

	private $from_name;
	private $from_mail;

	public function __construct($recipient, $subject, $message, $from_name = '', $from_mail = '')
	{
		$this->mail_recipients = array();
		$this->mail_recipients[] = $recipient;

		$this->mail_headers = array();
		$this->mail_subject = $subject;
		$this->mail_priority = self::PRIORITY_NORMAL;
		$this->mail_message = $message;

		$this->from_name = $from_name;
		$this->from_mail = $from_mail;
	}

	public function addCarbonCopy($email)
	{
		$this->addHeader("CC: {$email}");

		return true;
	}

	public function addBlindCarbonCopy($email)
	{
		$this->addHeader("BCC: {$email}");

		return true;
	}

	public function addHeader($header)
	{
		$this->mail_headers[] = $header;

		return true;
	}

	private function compileHeaders()
	{
		$headers = '';

		foreach($this->mail_headers as $header)
		{
			$headers .= $header . self::CLRF;
		}

		return $headers;
	}

	public function send($type = 'html')
	{
		if($this->from_name && $this->from_mail)
		{
			$this->addHeader("From: {$this->from_name} <{$this->from_mail}>");
			$this->addHeader("Reply-To: {$this->from_name} <{$this->from_mail}>");
		}
		else
		{
			$this->addHeader("From: {$this->from_name}");
			$this->addHeader("Reply-To: {$this->from_name}");
		}

		$this->addHeader("MIME-Version: 1.0");
		$this->addHeader("X-Mailer: Cogsworth Mailer");
		$this->addHeader("X-Priority: {$this->mail_priority}");

		$type == 'html' ? $this->addHeader("Content-type: text/html") : $this->addHeader("Content-type: text/plain");

		$this->addHeader("Mailed-by: Cogsworth");

		return mail(implode(', ', $this->mail_recipients), $this->mail_subject, $this->mail_message, $this->compileHeaders());
	}
}

?>